<?php
/*
*用於開發者後臺 加載module信息和 安裝module By yan
*/
namespace yan\backendclient;
!defined('DS') ? define('DS', DIRECTORY_SEPARATOR) : '';

class LoadConfig{
    private $path;
    private $namespace;
    private $basePath;
    private $method = '';

    public function __construct($basePath=null){
        $basePath = empty($basePath) ? dirname(__DIR__) : $basePath; 
        $this->basePath = $basePath;

        $this->path = $basePath.DS.'modules';
        $this->namespace = 'backend\modules\\';
    }

    // 查找文件信息
    protected function searchDir($path,&$data,$levevl=0,$path_name='',$flag='', $searchAll=false, $callBack=null){
        // 當二級目錄深度
        if( is_dir($path) && $levevl!=2 && !$searchAll ){
            $dp = dir($path);

            if($levevl==1){
                $path_name = basename($path);
            }

            $levevl++;
            while($file=$dp->read()){

                if($file!='.'&& $file!='..'){

                    $this->searchDir($path.DS.$file,$data,$levevl,$path_name,$flag);
                }
            }
            $dp->close();
        }else if( is_dir($path) && $searchAll && $levevl!==$searchAll ){
            $dp = dir($path);
            $levevl++;

            $path_name .= '/'. basename($path);
                
            while($file=$dp->read()){

                if($file!='.'&& $file!='..'){

                    $this->searchDir($path.DS.$file,$data,$levevl,$path_name,$flag, $searchAll, $callBack);
                }
            }
            $dp->close();
        }
        if(is_file($path)){

            $path = basename($path,".php");
            if($flag=='modules'){
                if(strpos($path,'Module')!==false){
                   $class_exists = $this->namespace.$path_name.'\\'.$path;

                    if(class_exists($class_exists)){
                        $data[$path_name] = ['class'=>$class_exists];
                    } 
                }
                
            }elseif($flag=='extensions'){
                $class_exists = $this->namespace.$path_name.'\DefaultController';

                if(class_exists($class_exists)){
                    $data['ext_'.$path_name] = ['class'=>$class_exists];
                }

            }elseif( !empty($callBack) && gettype($callBack)=='object' ){

                $callBack($data, $path, $path_name);
        
            }else{
                $data[] = ['path'=>$path_name,'name'=>$this->namespace.$path_name.'\\'.$path];
            }
        }
    }

    public function getDir(){
        $dir = $this->path;
        $data=array();
        $this->searchDir($dir,$data);
        return   $data;
    }
    // 获取 模块的信息
    public function getModules(){
        $this->setPath($this->basePath.DS.'modules');
        $this->setNamespace('backend\modules\\');
        $dir = $this->path;
        $data=array();
        $this->searchDir($dir,$data,0,'','modules');
        return   $data;
    }

    public function getMap(){
        $this->setPath($this->basePath.DS.'extensions');
        $this->setNamespace('backend\extensions\\');
        $dir = $this->path;
        $data=array();
        $this->searchDir($dir,$data,0,'','extensions');
        return   $data;
    }

    // 设置参数
    public function setNamespace($namespace){
        $this->namespace = $namespace;
    }

    // 设置路径
    public function setPath($path){
        $this->path = $path;
    }

    // 获取文件夹内的文件
    // 查找文件信息
    protected function searchFile($path,&$data,$levevl=0,$path_name='',$flag=''){
        if(is_dir($path)){
            $dp = dir($path);

            $path_name = basename($path);
            // echo $path;
            $levevl++;

            while($file=$dp->read()){
                if($file!='.'&& $file!='..'){
                    $this->searchFile($path.DS.$file,$data,$levevl,$path_name,$flag);
                }
            }
            $dp->close();
        }

        if(is_file($path)){

            if(call_user_func([$this,$this->method], $path)){
                $root_url = dirname(\Yii::$app->basePath);
                $class = str_replace([$root_url.'/','.php'], '' ,$path);
                $name = substr($class, strripos($class, '\\')+1);

                $data[] = ['name'=>$name,
                            'path'=>$path,
                            'class'=>$class]; 
            }
            
        }
    }

    // 查找controller文件
    public function getController($path){
        $data = [];
        $this->method = 'isController';

        $this->searchFile($path,$data);
        return $data;
    }

    // 判断是否为控制器
    public function isController($name){
        if(strpos($name, 'Controller')!==false){
            return true;
        }
        return false;
    }

    // action 转成id
    public function actionFormat($action){
      $temp_array = array();
      for($i=0;$i<strlen($action);$i++){
        $ascii_code = ord($action[$i]);
        if($ascii_code >= 65 && $ascii_code <= 90){
          if($i == 0){
             $temp_array[] = chr($ascii_code + 32);
          }else{
            $temp_array[] = '-'.chr($ascii_code + 32);
          }
        }else{
          $temp_array[] = $action[$i];
        }
      }
      return implode('',$temp_array);
    }

    public function deltree($pathdir)
    {
        //echo $pathdir;//我调试时用的
        if($this->is_empty_dir($pathdir))//如果是空的
        {
            rmdir($pathdir);//直接删除
        }
        else
        {//否则读这个目录，除了.和..外
            $d=dir($pathdir);
            while($a=$d->read())
            {
                if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..')){unlink($pathdir.'/'.$a);}
                //如果是文件就直接删除
                if(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..'))
                {//如果是目录
                    if(!$this->is_empty_dir($pathdir.'/'.$a))//是否为空
                    {//如果不是，调用自身，不过是原来的路径+他下级的目录名
                        $this->deltree($pathdir.'/'.$a);
                    }
                    if($this->is_empty_dir($pathdir.'/'.$a))
                    {//如果是空就直接删除
                        rmdir($pathdir.'/'.$a);
                    }
                }
            }
            $d->close();  
                   
            //echo "必须先删除目录下的所有文件";//我调试时用的
        }
    }
    
    public function is_empty_dir($pathdir)
    {
        //判断目录是否为空，我的方法不是很好吧？只是看除了.和..之外有其他东西不是为空
        $d=@opendir($pathdir);
        if(!$d) return true;
        $i=0;
        while($a=readdir($d))
        {
            $i++;
        }
        closedir($d);
        if($i>2){return false;}
        else return true;
    }
}